<html>
	<head>
		<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
		<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
		<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
		<script src="/jquery-tmpl-master/jquery.tmpl.min.js"></script>
		<script src="/js/image_to_photo.js"></script>
		<style type="text/css">
			body, table tr td{
				font-size: 13px;
			}
		
			div.success{
				background-color: rgb(43, 224, 43);
			}
			div.failure{
				background-color: red;
			}
		</style>
	</head>
	<body>
		<div id="testResults"> </div>
		
		<script id="resultTemplate" type="text/x-jquery-tmpl">
				<div class="${success}">
					${testName}:<br/><br/>
					<table>
						<tr>
							<td>input: </td>
							<td>[imageLeft, imageWidth, imageTop, imageHeight, containerLeft, containerWidth, containerTop, containerHeight, realImageWidth, realImageHeight]</td>
						</tr>
						<tr>
							<td></td>
							<td>${input}</td>
						</tr>
					</table>
					<br/>
					${description}<br/><br/>
					output: [sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight]<br/>
					expected ${expected}, but was ${actual}<br/>
					<hr/>
				</div>
		</script>
		<script type="text/javascript">	
			function testCalculateDrawData(input, expected, testName, description){
				var result = calculateDrawData(input);
				
				var expectedValues = "";
				$.each(expected, function(index, val) {
					expectedValues = expectedValues + " ; " + val;
				});
				var actualValues = "";
				$.each(result, function(index, val) {
					actualValues = actualValues + " ; " + val;
				});
			
				if(arraysEqual(expected, result)){
					data = [
						{
							testName : testName,
							expected : expectedValues,
							actual : actualValues,
							description : description,
							input : input,
							success : "success"
						}
					];
				} else {
					data = [
						{
							testName : testName,
							expected : expectedValues,
							actual : actualValues,
							description : description,
							input : input,
							success : "failure"
						}
					];
				}
				$( "#resultTemplate" ).tmpl( data ).appendTo( "#testResults" );	
			}
			
			function arraysEqual(a, b) {
			  if (a === b) return true;
			  if (a == null || b == null) return false;
			  if (a.length != b.length) return false;

			  // If you don't care about the order of the elements inside
			  // the array, you should sort both arrays here.

			  for (var i = 0; i < a.length; ++i) {
				if (a[i] !== b[i]) return false;
			  }
			  return true;
			}
		</script>
		
		<script type="text/javascript">
			//imageLeft, imageWidth, imageTop, imageHeight, containerLeft, containerWidth, containerTop, containerHeight, realImageWidth, realImageHeight, canvasWidthInch, canvasHeightInch
			//return [sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight, dpi_x, dpi_y];
			//
			//
			var testName = "Test Real life 3";
			var description = "Real life capture of the input for the scenario with a non resize canvas of 10cmx5cm.";
			var input = [313.109375,960,141.875,720,524.125,378,347.375,189,960,720,4,2];
			var expectedOutput = [211.015625,205.5,378,189,0,0,378,189,(378/4),(189/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Real life 2";
			var description = "Real life capture of the input for the scenario with a non resize canvas of 5cmx5cm.";
			var input = [313.109375,960,141.875,720,618.609375,189,347.375,189,960,720,4,2];
			var expectedOutput = [305.5,205.5,189,189,0,0,189,189,(189/4),(189/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Real life 1";
			var description = "Real life capture of the input for the scenario of Test1.";
			var input = [205.109375,960,171.875,720,305.109375,800,271.875,600,960,720,4,2];
			var expectedOutput = [100,100, 800, 600, 0, 0, 800, 600,(800/4),(600/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			//
			//
			var testName = "Test Resized - Outside 16";
			var description = "";
			var input = [100,100,100,250,300,500,300,100,1000,2500,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized - Outside 15";
			var description = "";
			var input = [100,100,350,250,300,500,300,100,1000,2500,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized - Outside 14";
			var description = "";
			var input = [100,400,500,100,300,500,300,100,4000,1000,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized - Outside 13";
			var description = "";
			var input = [500,500,500,100,300,500,300,100,5000,1000,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized - Outside 12";
			var description = "";
			var input = [1000,200,350,250,300,500,300,100,2000,2500,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized - Outside 11";
			var description = "";
			var input = [1000,200,200,150,300,500,300,100,2000,1500,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized - Outside 10";
			var description = "";
			var input = [500,500,100,100,300,500,300,100,5000,1000,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized - Outside 9";
			var description = "";
			var input = [100,400,100,100,300,500,300,100,4000,1000,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized - Outside 8";
			var description = "";
			var input = [100,600,100,300,100,300,600,200,6000,3000,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized - Outside 7";
			var description = "";
			var input = [400,200,100,200,100,700,500,200,2000,2000,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized - Outside 6";
			var description = "";
			var input = [50,450,500,300,100,300,100,300,4500,3000,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized - Outside 5";
			var description = "";
			var input = [200,100,500,300,100,300,100,300,1000,3000,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized - Outside 4";
			var description = "";
			var input = [100,300,100,500,700,300,300,200,3000,5000,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized - Outside 3";
			var description = "";
			var input = [100,300,300,200,700,300,100,700,3000,2000,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized - Outside 2";
			var description = "";
			var input = [1000,100,300,200,200,600,200,600,1000,2000,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized - Outside 1";
			var description = "The original image is not resized, and is bigger than the canvas. The image starts at 100px left of the right border of" +
			"the canvas. The top, bottom and right border of the image are Not resized - Outside the canvas.";
			var input = [1000,1000,0,900,100,800,200,600,10000,9000,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			//
			//
			var testName = "Test Resized 15";
			var description = "";
			var input = [100,200,300,200,200,200,200,200,500,500,4,2];
			var expectedOutput = [250,0,250,250,0,100,100,100,(100/4),(100/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized 14";
			var description = "";
			var input = [200,200,200,200,100,200,100,200,500,500,4,2];
			var expectedOutput = [0,0,250,250,100,100,100,100,(100/4),(100/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized 13";
			var description = "";
			var input = [150,250,50,350,100,200,100,400,700,700,4,2];
			var expectedOutput = [0,100,420,600,50,0,150,300,(150/4),(300/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized 12";
			var description = "";
			var input = [50,150,50,150,100,200,100,200,300,300,4,2];
			var expectedOutput = [100,100,200,200,0,0,100,100,(100/4),(100/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized 11";
			var description = "";
			var input = [150,100,200,200,100,200,100,200,600,500,4,2];
			var expectedOutput = [0,0,600,250,50,100,100,100,(100/4),(100/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized 10";
			var description = "";
			var input = [50,350,200,200,100,200,100,200,600,500,4,2];
			var expectedOutput = [round(600/7),0,round(2400/7),250,0,100,200,100,(200/4),(100/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized 9";
			var description = "";
			var input = [200,200,150,100,100,200,100,200,400,300,4,2];
			var expectedOutput = [0,0,200,100,100,50,100,100,(100/4),(100/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized 8";
			var description = "";
			var input = [200,200,50,400,100,200,100,200,600,600,4,2];
			var expectedOutput = [0,75,300,300,100,0,100,200,(100/4),(200/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized 7";
			var description = "";
			var input = [150,100,95,210,100,200,100,200,300,300,4,2];
			var expectedOutput = [0, round((5/210)*300),300,(200/210)*300,50,0,100,200,(100/4),(200/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized 6";
			var description = "";
			var input = [50,300,150,100,100,200,100,200,600,300,4,2];
			var expectedOutput = [100,0,400,100,0,50,200,100,(200/4),(100/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized 5";
			var description = "";
			var input = [50,300,50,200,100,200,100,200,600,300,4,2];
			var expectedOutput = [100,75,400,225,0,0,200,150,(200/4),(150/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized 4";
			var description = "";
			var input = [150,100,50,150,100,200,100,200,300,300,4,2];
			var expectedOutput = [0,100,300,200,50,0,100,100,(100/4),(100/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized 3";
			var description = "";
			var input = [75,125,75,275,100,200,100,200,350,350,4,2];
			var expectedOutput = [70,round((25/275)*350),280,254.5454545454546,0,0,100,200,(100/4),(200/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized 2";
			var description = "";
			var input = [75,125,150,50,100,200,100,200,300,300,4,2];
			var expectedOutput = [60,0,240,50,0,50,100,50,(100/4),(50/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Resized 1";
			var description = "";
			var input = [70,10,15,75,50,150,10,90,490,295,4,2];
			var expectedOutput = [0,0,490,295,20,5,10,75,(490/4),(295/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			//
			//
			var testName = "Test Not resized - Outside 16";
			var description = "";
			var input = [100,100,100,250,300,500,300,100,100,250,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized - Outside 15";
			var description = "";
			var input = [100,100,350,250,300,500,300,100,100,250,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized - Outside 14";
			var description = "";
			var input = [100,400,500,100,300,500,300,100,400,100,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized - Outside 13";
			var description = "";
			var input = [500,500,500,100,300,500,300,100,500,100,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized - Outside 12";
			var description = "";
			var input = [1000,200,350,250,300,500,300,100,200,250,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized - Outside 11";
			var description = "";
			var input = [1000,200,200,150,300,500,300,100,200,150,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized - Outside 10";
			var description = "";
			var input = [500,500,100,100,300,500,300,100,500,100,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized - Outside 9";
			var description = "";
			var input = [100,400,100,100,300,500,300,100,400,100,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized - Outside 8";
			var description = "";
			var input = [100,600,100,300,100,300,600,200,600,300,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized - Outside 7";
			var description = "";
			var input = [400,200,100,200,100,700,500,200,200,200,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized - Outside 6";
			var description = "";
			var input = [50,450,500,300,100,300,100,300,450,300];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized - Outside 5";
			var description = "";
			var input = [200,100,500,300,100,300,100,300,100,300,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized - Outside 4";
			var description = "";
			var input = [100,300,100,500,700,300,300,200,300,500,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized - Outside 3";
			var description = "";
			var input = [100,300,300,200,700,300,100,700,300,200,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized - Outside 2";
			var description = "";
			var input = [1000,100,300,200,200,600,200,600,100,200,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized - Outside 1";
			var description = "The original image is not resized, and is bigger than the canvas. The image starts at 100px left of the right border of" +
			"the canvas. The top, bottom and right border of the image are Not resized - Outside the canvas.";
			var input = [1000,1000,0,900,100,800,200,600,1000,900,4,2];
			var expectedOutput = [0,0,0,0,0,0,0,0,0,0];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			//
			//
			var testName = "Test Not resized 17";
			var description = "";
			var input = [200,100,200,600,100,300,400,200,100,600,4,2];
			var expectedOutput = [0,200,100,200,100,0,100,200,(300/4),(200/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized 16";
			var description = "";
			var input = [100,300,400,200,200,100,200,600,300,200,4,2];
			var expectedOutput = [100,0,100,200,0,200,100,200,(100/4),(600/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized 15";
			var description = "";
			var input = [300,400,300,200,100,800,200,600,400,200,4,2];
			var expectedOutput = [0,0,400,200,200,100,400,200,(800/4),(600/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized 14";
			var description = "";
			var input = [50,450,500,500,100,800,200,600,450,500,4,2];
			var expectedOutput = [50,0,400,300,0,300,400,300,(800/4),(600/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized 13";
			var description = "";
			var input = [500,500,500,500,100,800,200,600,500,500,4,2];
			var expectedOutput = [0,0,400,300,400,300,400,300,(800/4),(600/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized 12";
			var description = "";
			var input = [500,500,100,600,100,800,200,600,500,600,4,2];
			var expectedOutput = [0,100,400,500,400,0,400,500,(800/4),(600/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized 11";
			var description = "";
			var input = [50,450,100,400,100,800,200,600,450,400,4,2];
			var expectedOutput = [50,100,400,300,0,0,400,300,(800/4),(600/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized 10";
			var description = "";
			var input = [300,400,500,500,100,800,200,600,400,500,4,2];
			var expectedOutput = [0,0,400,300,200,300,400,300,(800/4),(600/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized 9";
			var description = "";
			var input = [50,950,500,500,100,800,200,600,950,500,4,2];
			var expectedOutput = [50,0,800,300,0,300,800,300,(800/4),(600/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized 8";
			var description = "";
			var input = [200,300,100,200,100,800,200,600,300,200,4,2];
			var expectedOutput = [0,100,300,100,100,0,300,100,(800/4),(600/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized 7";
			var description = "";
			var input = [50,950,100,400,100,800,200,600,950,400,4,2];
			var expectedOutput = [50,100,800,300,0,0,800,300,(800/4),(600/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized 6";
			var description = "";
			var input = [50,250,300,200,100,800,200,600,250,200,4,2];
			var expectedOutput = [50,0,200,200,0,100,200,200,(800/4),(600/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized 5";
			var description = "";
			var input = [50,250,100,900,100,800,200,600,250,900,4,2];
			var expectedOutput = [50,100,200,600,0,0,200,600,(800/4),(600/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized 4";
			var description = "";
			var input = [200,1000,300,200,100,800,200,600,1000,200,4,2];
			var expectedOutput = [0,0,700,200,100,100,700,200,(800/4),(600/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized 3";
			var description = "The original image is not resized, and is bigger than the canvas. The image starts at 100px left of the left border of" +
			"the canvas. The top, bottom and right border of the image are Not resized - Outside the canvas.";
			var input = [200,1000,5,900,100,800,200,600,1000,900,4,2];
			var expectedOutput = [0,195,700,600,100,0,700,600,(800/4),(600/2)];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized 2";
			var description = "The original image is not resized, but is bigger than the canvas (the canvas is fully surrounded by the image. " +
			"The expected result is that the image is not resized and that the part laying inside the boundaries of the canvas is returned";
			var input = [5,1000,5,900,100,800,200,600,1000,900,4,2];
			var expectedOutput = [95,195,800,600,0,0,800,600,200,300];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			var testName = "Test Not resized 1";
			var description = "The orginal image has the same dimensions as the canvas and the left and top offset of image and canvas are collapsing. " +
			"The expected result is that we get the dimensions of the image/canvas (as it's the same) with x and y start equal to '0'. " +
			"Source width and height will be equal to dest width and height and equal to original width and height, as no transformation (resize/move occurred)";
			var input = [10,200,20,100,10,200,20,100,200,100,3.9,2];
			var expectedOutput = [10,20,200,100,0,0,200,100,51.2820512820513,50];
			testCalculateDrawData(input, expectedOutput, testName, description);
			//
			
			function round(number){
				return Math.round(number * 10000000000000) / 10000000000000;
			}
		</script>
	</body>
</html>